<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Инсталляция Баз Данных</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<meta name="generator" content="Geany 0.18" />
</head>

<body>

<?php
/*
 *      install.php
 *      
 *      Copyright 2010 Rakitin Vitaliy <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

require_once("./classes/DB/DB.php");
require_once("./config.php");
require_once("./classes/system.php");
require_once("./classes/System/osmetadata.php");


function makeDataAtModel($uuid) {
	$sql = "SELECT * FROM fields WHERE owner = '$uuid';";
	$result = DB::select($sql);
	$sql = "CREATE TABLE $uuid (";
	$sql .= "id varchar(36) PRIMARY KEY, owner varchar(36),";
	foreach($result as $v) {
		switch ($v['type']) {
			case "float":{$sql .= " ".$v['id']." ".$v['type']."(".$v['length'].")"; break;}
			case "link":{$sql .= " ".$v['id']." varchar(36)"; break;}
			case "date":{$sql .= " ".$v['id']." ".$v['type']; break;}
			case "varchar":{$sql .= " ".$v['id']." ".$v['type']."(".$v['length'].")"; break;}
		}
		$sql .= ",";
	}
	$sql = substr_replace($sql, ");", -1, 1);
	print_r($sql); echo "<br><br>";
	DB::query($sql);
}

// Remove all tables in DB
if (isset($_POST['dropall'])) {
	$sql = "SELECT table_name FROM information_schema.tables WHERE table_schema='test';";
	$result = DB::select($sql);
	foreach($result as $v) {
		$sql = "DROP TABLE ".$v['table_name']." CASCADE";
		DB::query($sql);
	}
}

if (isset($_POST['install'])) {

	// Создаем таблицу "objects" содержащую информацию о объектах
	$sql = "CREATE TABLE objects (id varchar(36) PRIMARY KEY, owner varchar(36), name varchar(64), type varchar(64), description varchar(64), defaultlistform varchar(36), defaultitemform varchar(36), defaultchoiceform varchar(36));";
	print_r($sql); echo "<br><br>";
	DB::query($sql);
	
	// Создаем таблицу "fields" содержащую информацию о полях объекта
	$sql = "CREATE TABLE fields (id varchar(36) PRIMARY KEY, owner varchar(36), name varchar(64), type varchar(32), length varchar(8), ordered varchar(3), luobject varchar(36), lukey varchar(36), ludisplay varchar(36), onchange text(65535));";
	print_r($sql); echo "<br><br>";
	DB::query($sql);
	
	// Создаем таблицу "forms" содержащую информацию о формах объекта
	$sql = "CREATE TABLE forms (id varchar(36) PRIMARY KEY, owner varchar(36), name varchar(64), description varchar(64), type varchar(32), body text(65535));";
	print_r($sql); echo "<br><br>";
	DB::query($sql);
	
	// Создаем таблицу "is_edit_row" содержащую информацию о редактируемых записях
	$sql = "CREATE TABLE is_edit_row (table_id varchar(36), row_id varchar(36) PRIMARY KEY, user_id varchar(36), time_lock timestamp);";
	print_r($sql); echo "<br><br>";
	DB::query($sql);
	
	$dic = $metadata->objects->add("osdictionary");
	$dic->name = "Товар";
	$dic->save();
	$field = $dic->fields->add();
	$field->name = "Наименование";
	$field->type = "varchar";
	$field->length = 64;
	$field->save();
	$field = $dic->fields->add();
	$field->name = "ЦенаП";
	$field->type = "float";
	$field->length = "7,2";
	$field->onchange = '$this->item->fields->byname("ЦенаР")->sense = $sense*1.25;';
	$field->save();
	$field = $dic->fields->add();
	$field->name = "ЦенаР";
	$field->type = "float";
	$field->length = "7,2";
	$field->save();
	makeDataAtModel($dic->uuid);
	
	$dic = $metadata->objects->add("osdictionary");
	$dic->name = "Пол";
	$dic->save();
	$field = $dic->fields->add();
	$field->name = "Значение";
	$field->type = "varchar";
	$field->length = 64;
	$field->save();
	makeDataAtModel($dic->uuid);
	$luobject = $dic->uuid;
	$ludisplay = $field->uuid;
	
	$dic = $metadata->objects->add("osdictionary");
	$dic->name = "Контрагенты";
	$dic->save();
	$field = $dic->fields->add();
	$field->name = "Наименование";
	$field->type = "varchar";
	$field->length = 64;
	$field->save();
	$field = $dic->fields->add();
	$field->name = "Телефон";
	$field->type = "varchar";
	$field->length = 64;
	$field->save();
	makeDataAtModel($dic->uuid);
	
	$dic = $metadata->objects->add("osdictionary");
	$dic->name = "Сотрудники";
	$dic->save();
	$field = $dic->fields->add();
	$field->name = "Подпись";
	$field->type = "varchar";
	$field->length = 128;
	$field->save();
	$field = $dic->fields->add();
	$field->name = "Фамилия";
	$field->type = "varchar";
	$field->length = 64;
	$field->onchange = '$this->item->fields->byname("Подпись")->sense = $value." ".$this->item->fields->byname("Имя")->sense." ".$this->item->fields->byname("Отчество")->sense;';
	$field->save();
	$field = $dic->fields->add();
	$field->name = "Имя";
	$field->type = "varchar";
	$field->length = 64;
	$field->onchange = '$this->item->fields->byname("Подпись")->sense = $this->item->fields->byname("Фамилия")->sense." ".$value." ".$this->item->fields->byname("Отчество")->sense;';
	$field->save();
	$field = $dic->fields->add();
	$field->name = "Отчество";
	$field->type = "varchar";
	$field->length = 64;
	$field->onchange = '$this->item->fields->byname("Подпись")->sense = $this->item->fields->byname("Фамилия")->sense." ".$this->item->fields->byname("Имя")->sense." ".$value;';
	$field->save();
/*
	$field = $dic->fields->add();
	$field->name = "Год рождения";
	$field->type = "date";
	$field->save();
*/
	$field = $dic->fields->add();
	$field->name = "Пол";
	$field->type = "link";
	$field->luobject = $luobject;
	$field->ludisplay = $ludisplay;
	$field->save();
	makeDataAtModel($dic->uuid);
	
	$table = $dic->tables->add();
	$table->name = "Семья";
	$table->save();
	$field = $table->fields->add();
	$field->name = "Имя";
	$field->type = "varchar";
	$field->length = 64;
	$field->save();
	$field = $table->fields->add();
	$field->name = "Пол";
	$field->type = "link";
	$field->luobject = $luobject;
	$field->ludisplay = $ludisplay;
	$field->save();
	makeDataAtModel($table->uuid);
	

	$doc = $metadata->objects->add("osdocument");
	$doc->name = "ПриходнаяНакладная";
	$doc->save();
	$field = $doc->fields->add();
	$field->name = "Номер";
	$field->type = "varchar";
	$field->length = 32;
	$field->save();
	$field = $doc->fields->add();
	$field->name = "Дата";
	$field->type = "date";
	$field->save();
	$field = $doc->fields->add();
	$field->name = "Поставщик";
	$field->type = "link";
	$field->luobject = $metadata->objects->byname("Контрагенты")->uuid;
	$field->ludisplay = $metadata->objects->byname("Контрагенты")->fields->byname("Наименование")->uuid;
	$field->save();
	makeDataAtModel($doc->uuid);
	
	$table = $doc->tables->add();
	$table->name = "Товар";
	$table->save();
	$field = $table->fields->add();
	$field->name = "Наименование";
	$field->type = "link";
	$field->luobject = $metadata->objects->byname("Товар")->uuid;
	$field->ludisplay = $metadata->objects->byname("Товар")->fields->byname("Наименование")->uuid;
	$field->onchange = '$this->item->fields->byname("Цена")->sense = $metadata->objects->byuuid($this->luobject)->items->byuuid($sense)->fields->byname("ЦенаП")->sense;';
	$field->save();
	$field = $table->fields->add();
	$field->name = "Кол-во";
	$field->type = "varchar";
	$field->length = 10;
	$field->save();
	$field = $table->fields->add();
	$field->name = "Цена";
	$field->type = "float";
	$field->length = "7,2";
	$field->save();
	$field = $table->fields->add();
	$field->name = "Сумма";
	$field->type = "float";
	$field->length = "9,2";
	$field->save();
	makeDataAtModel($table->uuid);

	
/*
	$reg = $metadata->objects->add("osaccregister");
	$reg->name = "Обороты";
	$reg->save();
	makeDataAtModel($reg->uuid);
*/

}
?>

<form method='POST' action=''>
	<input type='submit' name='install' value='Install'><br>
	<input type='submit' name='dropall' value='Drop all'>
</form>
</body>
</html>
